package com.zhuqi;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * @author : ZhuQi
 * @version : 时间:2022/3/21 11:23 版本:
 * 排他网关测试
 */
public class ActivitiTestDemo03 {

    private ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();


    /**
     * 测试内容: 部署流程
     * 测试结果:
     */
    @Test
    public void test1() throws Exception {
        RepositoryService repositoryService = processEngine.getRepositoryService();

        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("bpmn/evecation-exclusive.bpmn")
                .addClasspathResource("bpmn/evecation-exclusive.png")
                .name("evecation-exclusive").deploy();


        System.out.println("部署ID" + deploy.getId());
        System.out.println("部署名" + deploy.getName());
    }


    /**
     * 测试内容: 启动流程实例
     * 测试结果:
     */
    @Test
    public void test02() throws Exception {
        RuntimeService runtimeService = processEngine.getRuntimeService();

        Map<String, Object> data = new HashMap<>();

        data.put("user0", "张三");
        data.put("user1", "李四");
        data.put("user2", "王五");
        data.put("user3", "赵六");
        data.put("day", "5");

        ProcessInstance processInstance =
                runtimeService.startProcessInstanceByKey("evecation-exclusive"
                        , "businessKey"
                        , data);


        System.out.println("流程实例ID" + processInstance.getId());
        System.out.println("流程实例名称" + processInstance.getName());
        System.out.println("流程变量" + processInstance.getProcessVariables());
    }

    /**
     * 测试内容: 查询任务并完成
     * 测试结果:
     */
    @Test
    public void test() throws Exception {
        String assignee = "李四";
        TaskService taskService = processEngine.getTaskService();

        Task task = taskService.createTaskQuery().processDefinitionKey("evecation-exclusive")
                .taskAssignee(assignee).singleResult();

        if (task != null) {
            System.out.println("查询到" + assignee + "的任务");
            System.out.println("任务名" + task.getName());
            System.out.println("任务处理人" + task.getAssignee());

            taskService.complete(task.getId());
            System.out.println(assignee + "完成了任务");
        } else {
            System.out.println(assignee + "没有任务");
        }
    }



}
